{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "aca8eb01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "36476837",
   "metadata": {},
   "outputs": [],
   "source": [
    "score_c1 = np.random.randint(30,100,size = (50,3))\n",
    "score_c2 = np.random.randint(30,100,size = (50,3))\n",
    "score_c3 = np.random.randint(30,100,size = (50,3))\n",
    "score_c4 = np.random.randint(30,100,size = (50,3))\n",
    "score_c5 = np.random.randint(30,100,size = (50,3))\n",
    "score_c6 = np.random.randint(30,100,size = (50,3))#随机生成6个班的成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "b8a4a58d",
   "metadata": {},
   "outputs": [],
   "source": [
    "score = np.concatenate([score_c1,score_c2,score_c3,score_c4,score_c5,score_c6])#l六个班成绩叠加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "2e6d71cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "sex = np.random.randint(1,3,size = (300,1))#生成性别数组，1代表男，2代表女"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "909f1c76",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.concatenate([score,sex],axis = 1)#水平叠加分数和性别数组，生成data数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "b5cbd221",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame(data=data,columns=['python','数学','语文','性别'])#将数组转换成DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "959a0c24",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['性别'] = data['性别'].replace([1,2],['男','女'])#把1换成男，2换成女"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "195ce471",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>女</th>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>男</th>\n",
       "      <td>32</td>\n",
       "      <td>30</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    python  数学  语文\n",
       "性别                \n",
       "女       30  31  30\n",
       "男       32  30  30"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='性别').min()#最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "4f4fcefb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>女</th>\n",
       "      <td>99</td>\n",
       "      <td>99</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>男</th>\n",
       "      <td>99</td>\n",
       "      <td>99</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    python  数学  语文\n",
       "性别                \n",
       "女       99  99  99\n",
       "男       99  99  99"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='性别').max()#最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "c488ac19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>女</th>\n",
       "      <td>66.18</td>\n",
       "      <td>65.60</td>\n",
       "      <td>64.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>男</th>\n",
       "      <td>66.19</td>\n",
       "      <td>64.44</td>\n",
       "      <td>63.17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    python     数学     语文\n",
       "性别                      \n",
       "女    66.18  65.60  64.01\n",
       "男    66.19  64.44  63.17"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='性别').mean().round(2)#平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "a166569a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>女</th>\n",
       "      <td>21.54</td>\n",
       "      <td>19.67</td>\n",
       "      <td>19.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>男</th>\n",
       "      <td>19.14</td>\n",
       "      <td>20.55</td>\n",
       "      <td>20.19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    python     数学     语文\n",
       "性别                      \n",
       "女    21.54  19.67  19.64\n",
       "男    19.14  20.55  20.19"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='性别').std().round(2)#标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "8ebdfb4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>女</th>\n",
       "      <td>68.5</td>\n",
       "      <td>65.0</td>\n",
       "      <td>66.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>男</th>\n",
       "      <td>67.5</td>\n",
       "      <td>66.0</td>\n",
       "      <td>63.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    python    数学    语文\n",
       "性别                    \n",
       "女     68.5  65.0  66.0\n",
       "男     67.5  66.0  63.0"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(by='性别').median().round(2)#中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "574707f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
